package com.journey.org.ui.video.broadcast

import android.os.Bundle
import androidx.lifecycle.Observer
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.ActivityBroadcastBinding
import com.mvvmhabit.org.base.BaseActivity
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerManager
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard

/**
 * @Date: 2020/11/18 14:15
 * @Author:小妖
 * @Description: 视频播发器
 */
class BroadcastActivity : BaseActivity<ActivityBroadcastBinding, BroadcastViewModel>() {

    private var mJcVideoPlayer: JCVideoPlayerStandard? = null

    override fun layoutId(savedInstanceState: Bundle?): Int = R.layout.activity_broadcast

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<BroadcastViewModel> = BroadcastViewModel::class.java

    override fun initData() {
        super.initData()
        // 史上最简单的视频播发器
        initJCVideoPlayer()
       //  mViewModel?.videoUrlField?.call(intent.extras?.getString("url")!!)
        mViewModel?.videoUrlField?.call(intent.extras?.getString("url")!!)
        mViewModel?.videoContentField?.set(intent.extras?.getString("content"))
        mViewModel?.videoDesField?.set(intent.extras?.getString("dec"))
        mViewModel?.videoTitleField?.set(intent.extras?.getString("title"))
        if (intent.extras?.getString("type") == "recommend") {
            mViewModel?.loadVideoBodyData(intent.extras?.getString("id")!!)
        }
    }

    private fun initJCVideoPlayer() {
        mJcVideoPlayer = mBinding.layoutJcVideoPlayer
    }

    override fun initViewObservable() {
        super.initViewObservable()
        mViewModel?.videoUrlField?.observe(this, Observer { t ->
            mBinding.layoutJcVideoPlayer.setUp(
                t, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, ""
            )
            mBinding.layoutJcVideoPlayer.startVideo()
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        JCVideoPlayer.releaseAllVideos()
        JCVideoPlayerManager.completeAll()
    }
}
